<template>
  <div class="Box">
    <el-form
      ref="ruleFormRef"
      :inline="true"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="客户姓名" prop="name">
        <el-input v-model="ruleForm.name" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="客户电话" prop="callNumber">
        <el-input
          v-model="ruleForm.callNumber"
          type="text"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="客户性别" props="sex">
        <el-select v-model="ruleForm.sex" placeholder="sex">
          <el-option label="男" value="男" />
          <el-option label="女" value="女" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >Submit</el-button
        >
        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import axios from "axios";
import { selectCustomerApi } from "@/api/userApi";
const emit = defineEmits(["search-data"]);

const ruleFormRef = ref<FormInstance>();

const validateName = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("Please input the name"));
  } else callback();
};

const validateCallNumber = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("Please input the callNumber"));
  } else callback();
};

const ruleForm = reactive({
  name: "",
  callNumber: "",
  sex: "",
});

const rules = reactive<FormRules<typeof ruleForm>>({
  name: [{ validator: validateName, trigger: "blur" }],
  callNumber: [{ validator: validateCallNumber, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  const { name, callNumber, sex } = ruleForm;
  const data = {
    name,
    callNumber,
    sex,
  };
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      emit("search-data",data);
    } else {
      console.log("error submit!");
      return false;
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>
<style lang="scss" scoped>
.Box {
  width: 100%;
  height: 50px;
  background: #ccc;
  padding-top: 10px;
  line-height: 50px;
  margin-bottom: 20px;
}
</style>>

